import { USER_ORDER_STATUS_MAP } from "../../utils/constant";
import request from "../../utils/http";
import { UserOrderVo } from "../../utils/type";
import { anyObj, enter, promisify, wxToast } from "../../utils/util";


// pages/userOrderDetail/userOrderDetail.ts
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		userOrderDetail: <null | UserOrderVo>null,
		query: <anyObj>{},
		USER_ORDER_STATUS_MAP
	},

	/**
	 * 点击预约
	 */
	toSchedule() {
		enter({
			url: `/pages/chooseScheduleDate/chooseScheduleDate`,
			query: {
				outletId: this.data.userOrderDetail?.outletId,
				orderId: this.data.query.orderId,
				serviceId: this.data.userOrderDetail?.serviceId
			}
		});
	},

	/**
	 * 点击取消订单
	 */
	async cancelOrder() {
		try {
			const res = await promisify(wx.showModal, {
				title: '请输入退款原因',
				editable: true,
				placeholderText: '请输入'
			});
			if (!res.confirm) return;
			await request({
				url: '/api/customers/order/cancel',
				method: 'POST',
				data: {
					orderId: this.data.query.orderId,
					reason: res.content
				},
				errToast: true,
				showLoading: {title: '正在取消...'},
				contentTypeForm: true
			});
			wxToast({title: '取消成功', icon: 'success'});
			setTimeout(() => {
				this.getUserOrderDetail();
			}, 1000);
		} catch (error) {
			
		}
	},

	async getUserOrderDetail() {
		try {
			const userOrderDetail = await request({
				url: '/api/customers/order/getOneOrderDetail',
				method: 'GET',
				query: {
					orderId: this.data.query.orderId
				},
				errToast: true,
				showLoading: {}
			});
			this.setData({userOrderDetail});
		} catch (error) {
			
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(query: anyObj) {
		this.setData({query});
		if (query.orderId) {
			this.getUserOrderDetail();
		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {
		
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})